<template>
  <div>
    <div class="header_search_nav search_form">
      <div class="ipt_left">
        <el-form ref="queryForm" :model="queryParams" label-width="80px">
          <el-row>
            <el-col :span="5">
              <el-form-item prop="typeName" label="分类名称"
                ><el-input
                  class="w-200"
                  v-model="queryParams.typeName"
                  placeholder="请输入项目名称"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="btn_right">
        <button class="btn1" @click="handleQuery">筛选</button>
        <button class="btn2" @click="resetQuery">重置</button>
      </div>
    </div>
    <div class="table_card">
      <div class="table_card_top">
        <span class="solid_left"></span>
        <div class="table_card_title">项目门店分类</div>
        <div class="op_right">
          <button class="primary_button" @click="NewSort">新增分类</button>
        </div>
      </div>

      <div class="table_list">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="typeName" label="分类名称" width="300px">
          </el-table-column>
          <el-table-column prop="typeName" label="单项目提成" width="300px">
            <template slot-scope="scope">
              <div class="switch-style" @click="handleStatusChange(scope.row)">
                <!-- {{ scope.row.itemStatus }} -->
                <div class="SwitchBtn">
                  <el-switch
                    v-model="scope.row.itemStatus"
                    active-color="#13ce66"
                    inactive-color="#ccc"
                    active-value="Y"
                    inactive-value="N"
                  >
                  </el-switch>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="reorder" label="排序" width="300px">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="300px">
          </el-table-column>
          <el-table-column prop="remark" label="备注"> </el-table-column>
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="EditSort(scope.row)"
                >编辑</el-button
              >
              <span class="separate_right">|</span>
              <el-button
                size="mini"
                type="text"
                @click="DelSort(scope.row.itemStoreTypeId)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          :total="total"
          :page.sync="queryParams.pageNo"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>

      <!-- 项目分类 -->
      <el-dialog
        :visible.sync="projectSortdialogVisible"
        width="400px"
        :title="title"
        class="no-dialog-padding"
        :before-close="handleClose"
      >
        <span>
          <div class="source_box">
            <el-form
              ref="projectSortForm"
              :rules="projectSortRules"
              :model="projectSortForm"
            >
              <el-row>
                <el-col>
                  <el-form-item label="分类名称" prop="typeName">
                    <el-input
                      class="w-100"
                      v-model="projectSortForm.typeName"
                      placeholder="请输入分类名称"
                    ></el-input>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col>
                  <el-form-item label="排序" prop="reorder">
                    <el-input-number
                      class="w-100 text-left"
                      v-model="projectSortForm.reorder"
                      placeholder="请输入排序"
                      :controls="false"
                    ></el-input-number>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-form-item label="应用门店" prop="storeName">
                  <div @click="clikApplyStore">
                    <el-input
                      class="w-100 selsectbox"
                      v-model="projectSortForm.storeName"
                      placeholder="请选择应用门店"
                      readonly
                    ></el-input>
                  </div>
                </el-form-item>
              </el-row>
              <el-row>
                <el-form-item label="来源备注" prop="remark">
                  <el-input
                    type="textarea"
                    placeholder="请输入来源备注"
                    v-model="projectSortForm.remark"
                    maxlength="30"
                    show-word-limit
                  />
                </el-form-item>
              </el-row>
            </el-form>
          </div>
        </span>
        <span slot="footer" class="dialog-footer">
          <el-button class="cancel_btn" @click="handleClose">取 消</el-button>
          <el-button
            type="primary"
            @click="SortSubmit"
            :loading="projectSortLoading"
            >确 定</el-button
          >
        </span>
      </el-dialog>

      <stores ref="stores" @change="changeStores"></stores>
    </div>
  </div>
</template>
<script>
import {
  getProjectStoreSortPage,
  getProjectStoreSort,
  CreateProjectStoreSort,
  UpdateProjectStoreSort,
  DeleteProjectStoreSort,
  UpdateItemStatus,
} from "@/api/business/project";
import stores from "@/components/Stores/storeModel";
export default {
  name: "EryuoaStoreFrontendClientlist",
  components: { stores },

  data() {
    return {
      projectSortdialogVisible: false, // 项目分类
      projectSortLoading: false,
      loading: false,

      title: "",
      textarea: "",
      tableData: [],

      projectSortRules: {
        typeName: [
          { required: true, message: "请输入来源名称", trigger: "blur" },
        ],
        reorder: [
          { required: true, message: "请输入分类排序", trigger: "blur" },
        ],
        storeId: [
          { required: true, message: "请选择应用门店", trigger: "change" },
        ],
      },

      projectSortForm: {
        typeName: "",
        remark: "",
        reorder: "",
        itemStoreTypeId: "",
        storeName: "",
        itemStoreTypeBondList: [],
      },

      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
      },

      total: 0,
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    handleStatusChange(row) {
      let text = row.itemStatus === "Y" ? "关闭" : "开启";
      this.$modal.confirm("确认要" + text + "单项目提成吗?").then(() => {
        UpdateItemStatus({ id: row.itemStoreTypeId }).then(() => {
          this.getList();
        });
      });
    },
    clikApplyStore() {
      this.$nextTick(() => {
        this.$refs.stores.applyStore(
          this.projectSortForm.itemStoreTypeBondList
        );
        this.$refs.stores.getStoresList(); // 调用子组件的方法
      });
    },

    changeStores(res) {
      console.log(res);
      // console.log(this.projectSortForm.itemStoreTypeBondList);
      const { storeName, list } = res;
      this.projectSortForm.storeName = storeName;
      this.projectSortForm.itemStoreTypeBondList = list.map((item) => {
        return { id: item.id, name: item.name };
      });
    },

    // 项目分类
    getList() {
      this.loading = true;
      getProjectStoreSortPage(this.queryParams)
        .then((res) => {
          console.log(res);
          this.loading = false;
          const { code, data } = res;
          if (code === 0) {
            this.tableData = data.list;
            this.total = data.total;
          }
        })
        .catch((err) => {
          this.loading = false;
        });
    },

    SortSubmit() {
      // this.projectSortdialogVisible = false;
      this.$refs["projectSortForm"].validate((valid) => {
        if (valid) {
          if (!this.projectSortForm.itemStoreTypeId) {
            this.CreateSort();
          } else {
            this.UpdateSort();
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    getQuery() {
      const {
        typeName,
        reorder,
        remark,
        itemStoreTypeBondList,
        itemStoreTypeId,
      } = this.projectSortForm;

      let array = itemStoreTypeBondList.map((item) => {
        return {
          storeId: item.id,
        };
      });

      return {
        itemStoreTypeId: itemStoreTypeId,
        typeName: typeName,
        reorder: reorder,
        remark: remark,
        itemStoreTypeBondList: array,
      };
    },

    // 新增分类 按钮 确认
    CreateSort() {
      this.projectSortLoading = true;
      CreateProjectStoreSort(this.getQuery())
        .then((res) => {
          this.projectSortLoading = false;
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.handleClose();
          }
        })
        .catch((err) => {
          this.projectSortLoading = false;
        });
    },

    // 编辑
    UpdateSort() {
      this.projectSortLoading = true;
      UpdateProjectStoreSort(this.getQuery())
        .then((res) => {
          this.projectSortLoading = false;
          const { code } = res;
          if (code === 0) {
            this.getList();
            this.$modal.msgSuccess("操作成功");
            this.handleClose();
          }
        })
        .catch((err) => {
          this.projectSortLoading = false;
        });
    },

    handleClose() {
      this.resetFormData("projectSortForm");
      this.projectSortForm.itemStoreTypeId = "";
      this.$nextTick(() => {
        this.projectSortdialogVisible = false;
        this.projectSortLoading = false;
      });
    },

    // 新建分类
    NewSort() {
      this.projectSortdialogVisible = true;
      this.title = "新增项目门店分类";
      this.$nextTick(() => {
        this.projectSortForm.itemStoreTypeBondList = [];
      });
    },

    // 项目门店分类详情
    getProjectStoreSorts(itemStoreTypeId) {
      getProjectStoreSort({ id: itemStoreTypeId })
        .then((res) => {
          const { code, data } = res;
          if (code === 0) {
            this.projectSortForm.typeName = data.typeName;
            this.projectSortForm.remark = data.remark;
            this.projectSortForm.reorder = data.reorder;
            this.projectSortForm.itemStoreTypeId = data.itemStoreTypeId;
            let names = data.storeList.map((item) => {
              return item.name;
            });
            this.projectSortForm.storeName = names.join(",");
            this.projectSortForm.itemStoreTypeBondList = data.storeList.map(
              (item) => {
                return { id: item.id, name: item.name };
              }
            );
          }
        })
        .catch((err) => {});
    },

    // 编辑分类
    EditSort(row) {
      this.projectSortdialogVisible = true;
      this.title = "编辑项目门店分类";

      this.getProjectStoreSorts(row.itemStoreTypeId);
    },

    // 筛选
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetFormData("queryForm");
      this.handleQuery();
    },

    // 表单重置
    resetFormData(formName) {
      console.log(this.$refs[formName]);
      this.$refs[formName].resetFields();
    },

    // 删除
    DelSort(itemStoreTypeId) {
      this.$confirm("项目门店分类删除后将无法撤销, 是否删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteProjectStoreSort({ id: itemStoreTypeId }).then((res) => {
            const { code } = res;
            if (code === 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss" scoped>
.w-200 {
  width: 200px;
  background: #ffffff;
  border-radius: 4px;
}

.client_name {
  font-size: 14px;
  font-weight: 400;
  color: #0052d9;
}

.give {
  height: 40px;
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
  margin-top: 40px;
  line-height: 40px;
}

.el-form-item {
  margin-bottom: 20px;
}

.recharge_form {
  padding: 10px 24px;
}

.memberName {
  font-size: 14px;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.85);
}
.el_row_item {
  margin-bottom: 10px;
}
.el_row_date {
  margin-top: 20px;
}
.el_row_reason {
  margin-top: 10px;
}

.el_row_date {
  .el-form-item {
    margin-bottom: 0px;
  }
}
.separate_right {
  width: 1px;
  height: 14px;
  // background: #e9e9e9;
  margin-left: 12px;
  margin-right: 12px;
}

.source_box {
  margin: 0 24px;
}

.table_list {
  padding-bottom: 24px;

  .SwitchBtn {
    position: relative;
    &::after {
      position: absolute;
      content: "";
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      z-index: 1;
    }
  }
}
</style>
